﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using DevExpress.Utils;
using ECRMS.WinForm.BaseClass.Xml;

namespace ECRMS.WinForm
{
    public partial class LoginForm : BaseForm
    {
        public Core core = Core.getInstance();

        public LoginForm()
        {
            
            InitializeComponent();

            core.CreateWaitDialog("系统启动中...", "系统初始化");

            InitView();

            core.CloseWaitDialog();

            core.CoreLoginForm = this;
        }

        private void InitView()
        {
            this.Icon = ResourceImageHelper.CreateIconFromResources("ECRMS.WinForm.LayoutMainDemo.ico", typeof(BaseForm).Assembly);

            //标题、登录信息等的语言设置
            core.CoreUserMode = new XMLReader("CustomMode.xml", "UserMode");
            core.CoreLanguage = (LangEnum)Enum.Parse(typeof(LangEnum), core.CoreUserMode.GetChildNodeText("language"));
            
            
            ChangeLanguage(core.CoreLanguage);

            BaseForm.defaultLookAndFeel.LookAndFeel.SkinName = core.CoreUserMode.GetChildNodeText("style");
        }

        private void ChangeLanguage(LangEnum langEnum)
        {
            
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            login();
        }

        private void login()
        {
            try
            {
                core.CreateWaitDialog("正在登录,请稍候..", "用户登录", 150, 50, true, core.CoreLoginForm, core.CoreLoginForm);
                if (core.CoreUser.Login(txt_name.Text.Trim(), txt_pass.Text.Trim()))
                //if (core.CoreUser.Login("admin","111111"))
                {
                    core.CloseWaitDialog();
                    core.CoreLoginForm.Hide();
                    if (core.CoreMainForm == null)
                        core.CoreMainForm = new MainForm();
                    else
                        core.CoreMainForm.InitNavBar();
                    core.CoreMainForm.Show();
                }
                else
                {
                    core.CloseWaitDialog();
                    XtraMessageBox.Show(core.CoreLoginForm, "登录失败，用户民或者密码错误！", "帐号或者密码验证错误");
                }
            }
            catch (Exception)
            {
                core.CloseWaitDialog();
                XtraMessageBox.Show(core.CoreLoginForm, "服务连接失败！", "服务连接");
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LoginForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                login();
            }
        }
    }
}